package com.tao.utilslib.ui;

/*
 * date: 2017/9/11 11:15
 * author: tao
 */

import android.view.View;

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.tao.utilslib.data.Obj;


public class FragmentTools {


    public static void addFragment(FragmentActivity activity, Fragment fragment, int id) {
        addFragment(activity, fragment, id, fragment.getClass().getSimpleName());
    }

    public static void addFragment(FragmentActivity activity, Fragment fragment, int id, String tag) {
        if (Obj.isNULL(activity, fragment))
            return;
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(id, fragment, tag);
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }

    public static void replaceFragment(FragmentActivity activity, Fragment fragment, int id, String tag) {

        if (Obj.isNULL(activity, fragment))
            return;
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(id, fragment, tag);
//        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }

    public static void replaceFragment(FragmentActivity activity, Fragment fragment, int id) {
        replaceFragment(activity, fragment, id, fragment.getClass().getSimpleName());
    }

    public static void replaceFragmentAttechAnimation(FragmentActivity activity, Fragment fragment, int id, String tag, int in, int out) {
        if (Obj.isNULL(activity, fragment, tag))
            return;
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setCustomAnimations(in, out);
        transaction.replace(id, fragment, tag);
        transaction.commitAllowingStateLoss();
    }


    public static void hideFragment(FragmentActivity activity, Fragment fragment) {
        if (Obj.isNULL(activity, fragment))
            return;
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.hide(fragment);
        View parent = (View) fragment.getView().getParent();
        if (parent != null) {
            parent.setVisibility(View.GONE);
        }
        transaction.commitAllowingStateLoss();

    }

    public static void removeFragment(FragmentActivity activity, Fragment fragment) {
        if (Obj.isNULL(activity, fragment))
            return;
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.remove(fragment);
        transaction.commitAllowingStateLoss();

    }


    public static void showFragment(FragmentActivity activity, Fragment fragment) {
        if (Obj.isNULL(activity, fragment))
            return;
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.show(fragment);
        View parent = (View) fragment.getView().getParent();
        if (parent != null) {
            parent.setVisibility(View.VISIBLE);
        }
        transaction.commitAllowingStateLoss();
    }

    public static <T extends Fragment> T findFragmentByClass(FragmentActivity activity, Class<T> tClass) throws Exception {
        if (Obj.isNULL(activity, tClass))
            throw new Exception("activity or class wheel be not  null");
        return (T) findFragmentByClass(activity, tClass.getSimpleName());

    }

    public static Fragment findFragmentByClass(FragmentActivity activity, String tag) {

        return activity.getSupportFragmentManager().findFragmentByTag(tag);

    }

    public static Fragment findFragment(FragmentActivity activity, int id) {
        return activity.getSupportFragmentManager().findFragmentById(id);
    }

    public static void showSupportDialogFragment(FragmentActivity mcContext, DialogFragment dialog, String s) {
        FragmentManager supportFragmentManager = mcContext.getSupportFragmentManager();
        dialog.show(supportFragmentManager, s);
    }

    public static void showDialogFragment(FragmentActivity mcContext, android.app.DialogFragment dialog, String s) {
        android.app.FragmentManager fragmentManager = mcContext.getFragmentManager();
        dialog.show(fragmentManager, s);
    }

    public static void dimissDialogFragment(android.app.DialogFragment dialog) {
        if (dialog != null && dialog.getDialog() != null && dialog.getDialog().isShowing())
            dialog.dismiss();
    }

    public static void dimissDialogFragmentByTag(FragmentActivity mcContext, String tag) {
        android.app.FragmentManager fragmentManager = mcContext.getFragmentManager();
        android.app.DialogFragment fragmentByTag = (android.app.DialogFragment) fragmentManager.findFragmentByTag(tag);
        if (fragmentByTag != null && fragmentByTag.getDialog() != null && fragmentByTag.getDialog().isShowing())
            fragmentByTag.dismiss();
    }

    public static void dimissSupportDialogFragmentByTag(FragmentActivity mcContext, String tag) {
        FragmentManager supportFragmentManager = mcContext.getSupportFragmentManager();
        DialogFragment fragmentByTag = (DialogFragment) supportFragmentManager.findFragmentByTag(tag);
        try {
            if (fragmentByTag != null && fragmentByTag.getDialog() != null && fragmentByTag.getDialog().isShowing())
                fragmentByTag.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void dimissSupportDialogFragment(DialogFragment dialog) {
        if (dialog != null && dialog.getDialog() != null && dialog.getDialog().isShowing())
            dialog.dismiss();
    }


    public static void addChaildFragment(Fragment fromFragment, Fragment toFragmen, int contanerId) {
        FragmentTransaction transaction = fromFragment.getChildFragmentManager().beginTransaction();
        transaction.add(contanerId, toFragmen, toFragmen.getClass().getSimpleName());
        transaction.commitAllowingStateLoss();
    }

    public static void addChaildFragment(Fragment fromFragment, Fragment toFragmen, int contanerId, String tag) {
        FragmentTransaction transaction = fromFragment.getChildFragmentManager().beginTransaction();
        transaction.add(contanerId, toFragmen, tag);
        transaction.commitAllowingStateLoss();
    }

    public static void addChaildFragment(Fragment fromFragment, Fragment toFragmen, String tag) {
        FragmentTransaction transaction = fromFragment.getChildFragmentManager().beginTransaction();
        transaction.add(toFragmen, tag);
        transaction.commitAllowingStateLoss();
    }
}
